package com.ruoyi.device.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.device.domain.DeviceInfo;
import com.ruoyi.device.domain.UserAuthStatus;
import com.ruoyi.device.domain.vo.DeviceMonitorVO;
import com.ruoyi.device.service.IDeviceMonitorService;
import com.ruoyi.device.service.IUserAuthStatusService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 设备监控Controller
 * 
 * @author ruoyi
 * @date 2025-10-18
 */
@RestController
@RequestMapping("/monitor")
public class DeviceMonitorController extends BaseController
{
    @Autowired
    private IDeviceMonitorService deviceMonitorService;

    @Autowired
    private IUserAuthStatusService userAuthStatusService;

    /**
     * 查询设备监控列表
     */
    @RequiresPermissions("device:monitor:query")
    @GetMapping("/devices")
    public TableDataInfo listDevices(DeviceInfo deviceInfo)
    {
        startPage();
        List<DeviceMonitorVO> list = deviceMonitorService.selectDeviceMonitorList(deviceInfo);
        return getDataTable(list);
    }

    /**
     * 查询设备下的用户列表
     */
    @RequiresPermissions("device:monitor:users")
    @GetMapping("/users/{deviceId}")
    public TableDataInfo listUsers(@PathVariable String deviceId)
    {
        List<UserAuthStatus> list = userAuthStatusService.selectUserAuthStatusByDeviceId(deviceId);
        TableDataInfo dataTable = new TableDataInfo();
        dataTable.setCode(200);
        dataTable.setMsg("查询成功");
        dataTable.setRows(list);
        dataTable.setTotal(list.size());
        return dataTable;
    }

    /**
     * 获取用户详情
     */
    @RequiresPermissions("device:monitor:detail")
    @GetMapping("/user/{id}")
    public AjaxResult getUserDetail(@PathVariable Long id)
    {
        return success(userAuthStatusService.selectUserAuthStatusById(id));
    }

    /**
     * 踢出用户
     */
    @RequiresPermissions("device:monitor:kickout")
    @Log(title = "设备监控", businessType = BusinessType.UPDATE)
    @PutMapping("/kickout/{id}")
    public AjaxResult kickout(@PathVariable Long id)
    {
        return toAjax(userAuthStatusService.kickoutUser(id));
    }

    /**
     * 延长用户上网时间
     */
    @RequiresPermissions("device:monitor:extend")
    @Log(title = "设备监控", businessType = BusinessType.UPDATE)
    @PutMapping("/extend")
    public AjaxResult extendTime(@RequestBody UserAuthStatus userAuthStatus)
    {
        // userAuthStatus.remark 用于传递延长的分钟数
        Integer extendMinutes = Integer.parseInt(userAuthStatus.getRemark());
        return toAjax(userAuthStatusService.extendUserTime(userAuthStatus.getId(), extendMinutes));
    }

    /**
     * 拉黑用户
     */
    @RequiresPermissions("device:monitor:blacklist")
    @Log(title = "设备监控", businessType = BusinessType.UPDATE)
    @PutMapping("/blacklist")
    public AjaxResult blacklist(@RequestBody UserAuthStatus userAuthStatus)
    {
        return toAjax(userAuthStatusService.blacklistUser(userAuthStatus.getId(), userAuthStatus.getRemark()));
    }

    /**
     * 导出设备监控列表
     */
    @RequiresPermissions("device:monitor:export")
    @Log(title = "设备监控", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceInfo deviceInfo)
    {
        List<DeviceMonitorVO> list = deviceMonitorService.selectDeviceMonitorList(deviceInfo);
        ExcelUtil<DeviceMonitorVO> util = new ExcelUtil<DeviceMonitorVO>(DeviceMonitorVO.class);
        util.exportExcel(response, list, "设备监控数据");
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("device:monitor:export")
    @Log(title = "用户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export/users")
    public void exportUsers(HttpServletResponse response, UserAuthStatus userAuthStatus)
    {
        List<UserAuthStatus> list = userAuthStatusService.selectUserAuthStatusList(userAuthStatus);
        ExcelUtil<UserAuthStatus> util = new ExcelUtil<UserAuthStatus>(UserAuthStatus.class);
        util.exportExcel(response, list, "用户认证数据");
    }
}

